/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.entity.accessor.entity.AccessorCollection;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCollectionCreateNewGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCollectionCtorGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCollectionInitGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.AccessorCreateInstaceGenerator;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;

public class AccessorCollectionGenerator extends ClassGenerator
{
	private AccessorClassGenContext rootContext;
	//private readonly Type parentDataInterfaceType;
	private String objectCode;
	private MediateType accessorType;
	private String parentIdPropName;
	private MediateType parentInterfaceType;


	public AccessorCollectionGenerator(MediateType parentInterfaceType, AccessorClassGenContext context, String objectCode, MediateType accessorType, String parentIdPropName)
	{
		super(context.getEntityDataGenContext().getProjContext().getCefGenContext().getCoreGenContext().getCompilationUnitInfo());
		rootContext = context;
		this.parentInterfaceType = parentInterfaceType;
		this.objectCode = objectCode;
		this.accessorType = accessorType;
		this.parentIdPropName = parentIdPropName;
		rootContext.getAllTypeInfo().getChildObjAccessorCollectionTypes().put(objectCode, new MediateType(getNameSpace(), getName()));
	}


	@Override
	protected TypeInfo setParentClass() {
		return new TypeInfo(AccessorCollection.class.getTypeName());
	}

	@Override
	protected ClassGeneratorContext createClassInfoContext()
	{
		return new ClassGeneratorContext();
	}

	@Override
	protected java.util.ArrayList<ClassMethodGenerator> createConstructorGenerators()
	{
		ArrayList<ClassMethodGenerator> list =super.createConstructorGenerators();
		if(list==null)
			list=new ArrayList<>();
		list.add(new AccessorCollectionCtorGenerator(getName()));
		return list;
	}

	@Override
	protected java.util.ArrayList<ClassMethodGenerator> createMethodGenerators()
	{
		return new java.util.ArrayList<ClassMethodGenerator>(java.util.Arrays.asList(new ClassMethodGenerator[] { get_Initialize_Generator(), get_CreateInstance_Generator(),get_CreateNew_Generator() }));
	}
	private ClassMethodGenerator get_Initialize_Generator()
	{
		MediateType childDataIntefaceType = rootContext.getEntityDataGenContext().getProjContext().getCefGenContext().getEntityGenContext().getNodeTypes().get(objectCode);
		return new AccessorCollectionInitGenerator(parentInterfaceType, objectCode+"s", accessorType, childDataIntefaceType);
	}

	//protected override IAccessor createinstance()
	//public abstract IChildAccessor createinstance();
	private ClassMethodGenerator get_CreateInstance_Generator()
	{
		return new AccessorCreateInstaceGenerator(accessorType, rootContext, parentIdPropName, objectCode);
	}

	private AccessorCollectionCreateNewGenerator get_CreateNew_Generator() {
		return new AccessorCollectionCreateNewGenerator(getName());
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	private static final String NameTemplate = "%1$sAccessorCollection";
	@Override
	protected String getName()
	{
		return String.format(NameTemplate, objectCode);
	}

	protected String getNameSpace()
	{
		return "";
	}
}
